package com.frxuexi.user.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.room.AskLeave
import com.frxuexi.user.R
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import java.text.SimpleDateFormat

@Composable
fun UserAskDetailPage(navHostController: NavHostController,json:String) {
    val kv = MMKV.defaultMMKV()

    val name by remember {
        mutableStateOf(kv.decodeString("name", "未登录"))
    }
    val role by remember {
        mutableStateOf(kv.decodeInt("role", 0))
    }
    val leave = Gson().fromJson(json, AskLeave::class.java)
    Column {
        MyTitleBar(title = "请假审批", navHostController = navHostController)
        Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
            Text(text = "申报人信息", color = Color.Blue)
            ElevatedCard(
                modifier = Modifier.fillMaxWidth(),
                colors = CardDefaults.elevatedCardColors(containerColor = Color.White)
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(10.dp)
                ) {
                    AsyncImage(
                        model = "https://img1.baidu.com/it/u=3406115504,1068597125&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1725037200&t=3c5e09c88f402ee3f67ff412d5dae000",
                        contentDescription = "",
                        modifier = Modifier
                            .size(80.dp)
                            .clip(CircleShape),
                        contentScale = ContentScale.Crop
                    )
                    Spacer(modifier = Modifier.width(10.dp))
                    Column(
                        modifier = Modifier.height(80.dp),
                        verticalArrangement = Arrangement.SpaceAround
                    ) {
                        Text(text = name!!)
                        Text(text = if (role == 1) "经理" else if (role == 2) "产品经理" else if (role == 3) "员工" else "")
                    }
                }
            }
            Text(text = "请假详情", color = Color.Blue)
            Text(text = "请假类型：${leave.type}")
            Text(text = "请假方式：${leave.method}")
            Text(text = "开始时间：${leave.start}")
            Text(text = "结束时间：${leave.end}")
            Text(text = "请假原因：${leave.reason}")


            Row(Modifier.padding(10.dp)) {
                Column(horizontalAlignment = Alignment.CenterHorizontally) {
                    AsyncImage(
                        model = "https://img1.baidu.com/it/u=3406115504,1068597125&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1725037200&t=3c5e09c88f402ee3f67ff412d5dae000",
                        contentDescription = "",
                        modifier = Modifier
                            .size(40.dp)
                            .clip(CircleShape),
                        contentScale = ContentScale.Crop
                    )
                    Divider(
                        Modifier
                            .height(30.dp)
                            .width(1.dp))
                    val id= if(leave.status=="同意") R.drawable.ic_action_success else if(leave.status=="拒绝") R.drawable.ic_action_refuse else R.drawable.ic_action_commit
                    Image(painter = painterResource(id = id), contentDescription = "",
                        modifier = Modifier
                            .size(40.dp),
                        contentScale = ContentScale.Crop)
                }
                Spacer(modifier = Modifier.width(10.dp))
                Column {
                    Text(text = "已提交")
                    val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(leave.time)
                    Text(text = "申报时间：$time", color = Color.Gray, fontSize = 12.sp)
                    Spacer(modifier = Modifier.height(30.dp))
                    val state=if(leave.status=="已提交") "审批中" else leave.status
                    Text(text = state!!)
                    val time2 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(leave.comTime)
                    val sptime=if(leave.status=="已提交") "" else time2
                    Text(text = "审批时间：$sptime", color = Color.Gray, fontSize = 12.sp)
                }
            }
        }
    }
}